package com.itheima.tanhua.apiImpl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.itheima.tanhua.Dto.LocationDto;
import com.itheima.tanhua.api.mongodb.BaiduLocationApi;
import com.itheima.tanhua.pojo.db.User;
import com.itheima.tanhua.pojo.mongo.UserLocation;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.geo.Circle;
import org.springframework.data.geo.Distance;

import org.springframework.data.geo.Metric;
import org.springframework.data.geo.Metrics;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.geo.GeoJsonPoint;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.List;


@DubboService
public class BaiduLocationApiImpl implements BaiduLocationApi {

    @Autowired
    private MongoTemplate mongoTemplate;

    //修改百度的地址数据
    public boolean updateLocation(LocationDto dto) {
        try {
            Query query = Query.query(Criteria.where("userId").is(dto.getUserId()));
            UserLocation location = mongoTemplate.findOne(query, UserLocation.class);
            if (ObjectUtil.isEmpty(location)){
                //如果地址不存在,保存地址
                UserLocation userLocation = new UserLocation();
                userLocation.setUserId(dto.getUserId());
                userLocation.setAddress(dto.getAddrStr());
                userLocation.setCreated(System.currentTimeMillis());
                userLocation.setUpdated(System.currentTimeMillis());
                userLocation.setLastUpdated(System.currentTimeMillis());
                userLocation.setLocation(new GeoJsonPoint(dto.getLongitude(), dto.getLatitude()));
                mongoTemplate.save(userLocation);
            }else {
                //3、如果存在，更新
                Update update = Update.update("localtion", new GeoJsonPoint(dto.getLongitude(), dto.getLatitude()))
                        .set("updated", System.currentTimeMillis())
                        .set("lastUpdated", location.getUpdated());
                mongoTemplate.upsert(query,update,UserLocation.class);
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
       //获取用户当前的位置
    public UserLocation findNowLocation(Long uid) {
        //构造查询的条件
        Query query=Query.query(Criteria.where("userId").is(uid));
        //查询当前用户的坐标原点
        return mongoTemplate.findOne(query,UserLocation.class);
    }

    //  TODO 附近的人 包括用户的位置  未测试成功
    @Override
    public List<Long> queryNearUser(Long uid, Double toDouble) {
        //获取用户的坐标
        Query query = Query.query(Criteria.where("userId").is(uid));
        UserLocation location = mongoTemplate.findOne(query, UserLocation.class);
        if (ObjectUtil.isEmpty(location)){
            return null;
        }
        //以用户的当前位置绘制原点
        GeoJsonPoint point = location.getLocation();
        //绘制半径
        Distance distance=new Distance(toDouble/1000, Metrics.KILOMETERS);
        //绘制圆
        Circle circle=new Circle(point,distance);
        //构造查询的条件
        Query locationQuery = Query.query(Criteria.where("location").withinSphere(circle));
        List<UserLocation> userLocations = mongoTemplate.find(locationQuery, UserLocation.class);
        return CollUtil.getFieldValues(userLocations, "userId",Long.class);
    }
}
